package com.jason.bbs.pms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.ws.rs.PathParam;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jason.bbs.bean.ResultJson;
import com.jason.bbs.bean.user.UserInfo;
import com.jason.bbs.pms.feign.UserService;

@RestController
public class PmsController {
	@Resource
	private UserService service;
	private Map<String, String> loginIds = new HashMap<>();

	@GetMapping("/check/{loginId}")
	public ResultJson checkLogin(@PathVariable("loginId") String loginId) {
		ResultJson code = new ResultJson(200, "成功");
		code.put("existis", loginIds.containsKey(loginId));
		return code;
	}

	@PostMapping("/login")
	public ResultJson login(String name, String password) {
		ResultJson code = new ResultJson(200, "成功");
		ResultJson rs = service.find(name, password);
		List<Object> list = rs.getValue("list");
		if (list == null || list.isEmpty()) {
			code.setCode(403);
			code.setMessage("用户名或密码错误");
			return code;
		} else {
			Map<String,Object> info = (Map<String, Object>) list.get(0);
			info.remove("password");
			code.put("user", info);
			String loginId=UUID.randomUUID().toString();
			loginIds.put(loginId, name);
			code.put("loginId",loginId );
		
		}
		return code;
	}
}
